

<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>geoslurp.datapull.thredds &mdash; geoslurp v1.1.0 documentation</title>
  

  
  
    <link rel="shortcut icon" href="../../../_static/favicon.ico"/>
  
  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../../../_static/css/theme.css" type="text/css" />
  

  

  
        <link rel="index" title="Index"
              href="../../../genindex.html"/>
        <link rel="search" title="Search" href="../../../search.html"/>
    <link rel="top" title="geoslurp v1.1.0 documentation" href="../../../index.html"/>
        <link rel="up" title="Module code" href="../../index.html"/> 

  
  <script src="../../../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

   
  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../../../index.html" class="icon icon-home"> geoslurp
          

          
          </a>

          
            
            
              <div class="version">
                1.1.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../../../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
              
            
            
              <p class="caption"><span class="caption-text">Contents:</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../../../introduction.html">An introduction to Geoslurp</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../installation.html">Installation of the geoslurp package and setting up the PostgreSQL instance</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../confsettings.html">Configuration</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../examples.html">Tutorial</a></li>
<li class="toctree-l1"><a class="reference internal" href="../../../reference.html">Reference Documentation</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        
          <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
          <a href="../../../index.html">geoslurp</a>
        
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          















<div role="navigation" aria-label="breadcrumbs navigation">

  <ul class="wy-breadcrumbs">
    
      <li><a href="../../../index.html">Docs</a> &raquo;</li>
        
          <li><a href="../../index.html">Module code</a> &raquo;</li>
        
      <li>geoslurp.datapull.thredds</li>
    
    
      <li class="wy-breadcrumbs-aside">
        
            
        
      </li>
    
  </ul>

  
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <h1>Source code for geoslurp.datapull.thredds</h1><div class="highlight"><pre>
<span></span><span class="c1"># This file is part of geoslurp.</span>
<span class="c1"># geoslurp is free software; you can redistribute it and/or</span>
<span class="c1"># modify it under the terms of the GNU Lesser General Public</span>
<span class="c1"># License as published by the Free Software Foundation; either</span>
<span class="c1"># version 3 of the License, or (at your option) any later version.</span>

<span class="c1"># geoslurp is distributed in the hope that it will be useful,</span>
<span class="c1"># but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="c1"># MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU</span>
<span class="c1"># Lesser General Public License for more details.</span>

<span class="c1"># You should have received a copy of the GNU Lesser General Public</span>
<span class="c1"># License along with Frommle; if not, write to the Free Software</span>
<span class="c1"># Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA</span>

<span class="c1"># Author Roelof Rietbroek (roelof@geod.uni-bonn.de), 2018</span>

<span class="kn">from</span> <span class="nn">lxml</span> <span class="k">import</span> <span class="n">etree</span> <span class="k">as</span> <span class="n">XMLTree</span>
<span class="kn">from</span> <span class="nn">geoslurp.datapull.http</span> <span class="k">import</span> <span class="n">Uri</span> <span class="k">as</span> <span class="n">http</span>
<span class="kn">import</span> <span class="nn">re</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">from</span> <span class="nn">geoslurp.config.slurplogger</span> <span class="k">import</span> <span class="n">slurplogger</span>
<span class="kn">from</span> <span class="nn">collections</span> <span class="k">import</span> <span class="n">namedtuple</span>
<span class="kn">from</span> <span class="nn">dateutil.parser</span> <span class="k">import</span> <span class="n">parse</span> <span class="k">as</span> <span class="n">isoParser</span>

<span class="kn">from</span> <span class="nn">geoslurp.datapull</span> <span class="k">import</span> <span class="n">UriBase</span><span class="p">,</span><span class="n">CrawlerBase</span>


<div class="viewcode-block" id="Uri"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Uri">[docs]</a><span class="k">class</span> <span class="nc">Uri</span><span class="p">(</span><span class="n">UriBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Thredds URI class&quot;&quot;&quot;</span>
    <span class="n">suburl</span><span class="o">=</span><span class="kc">None</span>
    <span class="n">opendap</span><span class="o">=</span><span class="kc">None</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">dataxml</span><span class="p">,</span><span class="n">services</span><span class="p">):</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">suburl</span><span class="o">=</span><span class="n">dataxml</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s1">&#39;urlPath&#39;</span><span class="p">]</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">opendap</span><span class="o">=</span><span class="n">services</span><span class="o">.</span><span class="n">baseurl</span><span class="o">+</span><span class="n">services</span><span class="o">.</span><span class="n">opendap</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">suburl</span>
        <span class="c1">#set the main url to the http version</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">services</span><span class="o">.</span><span class="n">baseurl</span><span class="o">+</span><span class="n">services</span><span class="o">.</span><span class="n">http</span><span class="o">+</span><span class="bp">self</span><span class="o">.</span><span class="n">suburl</span><span class="p">,</span><span class="n">lastmod</span><span class="o">=</span><span class="n">getDate</span><span class="p">(</span><span class="n">dataxml</span><span class="p">))</span></div>




<div class="viewcode-block" id="ThreddsFilter"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.ThreddsFilter">[docs]</a><span class="k">class</span> <span class="nc">ThreddsFilter</span><span class="p">():</span>
    <span class="sd">&quot;&quot;&quot;Helper class to aid traversing to opendap xml elements&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">xmltyp</span><span class="o">=</span><span class="s2">&quot;*&quot;</span><span class="p">,</span><span class="n">attr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span><span class="n">regex</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sets up a filter. Note that the default xmltype of &#39;*&#39; accepts all elements&quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_type</span><span class="o">=</span><span class="n">xmltyp</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_orFilter</span><span class="o">=</span><span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_andFilter</span><span class="o">=</span><span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_attr</span><span class="o">=</span><span class="n">attr</span>
        <span class="k">if</span> <span class="n">regex</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="o">=</span><span class="n">re</span><span class="o">.</span><span class="n">compile</span><span class="p">(</span><span class="n">regex</span><span class="p">)</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="o">=</span><span class="kc">None</span>

<div class="viewcode-block" id="ThreddsFilter.isCatalog"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.ThreddsFilter.isCatalog">[docs]</a>    <span class="k">def</span> <span class="nf">isCatalog</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Check if the filter type is a catalogRef&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">==</span> <span class="s1">&#39;catalogRef&#39;</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">True</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="k">return</span> <span class="kc">False</span></div>

<div class="viewcode-block" id="ThreddsFilter.isValid"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.ThreddsFilter.isValid">[docs]</a>    <span class="k">def</span> <span class="nf">isValid</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">xmlelem</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Filter xmlelem on  attributes&quot;&quot;&quot;</span>

        <span class="n">valid</span><span class="o">=</span><span class="kc">False</span>
        <span class="c1"># First test: Datatype test</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_type</span> <span class="o">==</span> <span class="s2">&quot;*&quot;</span> <span class="ow">or</span> <span class="n">xmlelem</span><span class="o">.</span><span class="n">tag</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_type</span><span class="p">):</span>
            <span class="n">valid</span><span class="o">=</span> <span class="kc">True</span>

        <span class="c1"># Second test: attribute test (but test only when datatype matches)</span>
        <span class="k">if</span> <span class="n">valid</span> <span class="ow">and</span> <span class="bp">self</span><span class="o">.</span><span class="n">_attr</span><span class="p">:</span>
            <span class="c1"># Also do an attribute test</span>
            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_attr</span> <span class="ow">in</span> <span class="n">xmlelem</span><span class="o">.</span><span class="n">attrib</span><span class="p">:</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="p">:</span>
                    <span class="n">valid</span><span class="o">=</span><span class="nb">bool</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_regex</span><span class="o">.</span><span class="n">match</span><span class="p">(</span><span class="n">xmlelem</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="bp">self</span><span class="o">.</span><span class="n">_attr</span><span class="p">]))</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># return True when no regex search is needed</span>
                    <span class="n">valid</span><span class="o">=</span><span class="kc">True</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="c1"># When the attribute is not found the test is considered as false</span>
                <span class="n">valid</span><span class="o">=</span><span class="kc">False</span>

        <span class="c1">#possibly apply chained OR and AND filters</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_orFilter</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">valid</span><span class="p">:</span>
            <span class="c1"># perform an additional test</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_orFilter</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">xmlelem</span><span class="p">)</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_andFilter</span> <span class="ow">and</span> <span class="n">valid</span><span class="p">:</span>
            <span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">_andFilter</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">xmlelem</span><span class="p">)</span>

        <span class="c1">#else return the result from the first 2 tests</span>
        <span class="k">return</span> <span class="n">valid</span></div>

<div class="viewcode-block" id="ThreddsFilter.OR"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.ThreddsFilter.OR">[docs]</a>    <span class="k">def</span> <span class="nf">OR</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xmltyp</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Provides a method for chaining OR filters&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_andFilter</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot apply AND and OR at the same time&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_orFilter</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="n">xmltyp</span><span class="p">,</span> <span class="n">attr</span><span class="p">,</span> <span class="n">regex</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span></div>

<div class="viewcode-block" id="ThreddsFilter.AND"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.ThreddsFilter.AND">[docs]</a>    <span class="k">def</span> <span class="nf">AND</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xmltyp</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Provides a method for chaining OR filters&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_orFilter</span><span class="p">:</span>
            <span class="k">raise</span> <span class="ne">ValueError</span><span class="p">(</span><span class="s2">&quot;Cannot apply AND and OR at the same time&quot;</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_andFilter</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="n">xmltyp</span><span class="p">,</span> <span class="n">attr</span><span class="p">,</span> <span class="n">regex</span><span class="p">)</span>
        <span class="k">return</span> <span class="bp">self</span></div></div>



<div class="viewcode-block" id="gethref"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.gethref">[docs]</a><span class="k">def</span> <span class="nf">gethref</span><span class="p">(</span><span class="nb">input</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;small function to extract a href link from a dictionary&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">ky</span><span class="p">,</span><span class="n">val</span> <span class="ow">in</span> <span class="nb">input</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">ky</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;href&quot;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">val</span>
    <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="getDate"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.getDate">[docs]</a><span class="k">def</span> <span class="nf">getDate</span><span class="p">(</span><span class="n">xml</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;extracts the date from a dataset element&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">xml</span><span class="p">:</span>
        <span class="k">if</span> <span class="n">elem</span><span class="o">.</span><span class="n">tag</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;date&quot;</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">isoParser</span><span class="p">(</span><span class="n">elem</span><span class="o">.</span><span class="n">text</span><span class="p">)</span><span class="o">.</span><span class="n">replace</span><span class="p">(</span><span class="n">tzinfo</span><span class="o">=</span><span class="kc">None</span><span class="p">)</span></div>

<div class="viewcode-block" id="getTagEnding"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.getTagEnding">[docs]</a><span class="k">def</span> <span class="nf">getTagEnding</span><span class="p">(</span><span class="n">xml</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Strip the leading junk ({...}) from a tag&quot;&quot;&quot;</span>
    <span class="n">ln</span><span class="o">=</span><span class="n">xml</span><span class="o">.</span><span class="n">tag</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;}&#39;</span><span class="p">)</span>
    <span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">ln</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ln</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
    <span class="k">else</span><span class="p">:</span>
        <span class="k">return</span> <span class="n">ln</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span></div>

<div class="viewcode-block" id="getAttrib"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.getAttrib">[docs]</a><span class="k">def</span> <span class="nf">getAttrib</span><span class="p">(</span><span class="n">xml</span><span class="p">,</span><span class="n">regex</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;Search in xml attributes based on a regex&quot;&quot;&quot;</span>
    <span class="k">for</span> <span class="n">ky</span><span class="p">,</span><span class="n">val</span> <span class="ow">in</span> <span class="n">xml</span><span class="o">.</span><span class="n">attrib</span><span class="o">.</span><span class="n">items</span><span class="p">():</span>
        <span class="k">if</span> <span class="n">re</span><span class="o">.</span><span class="n">search</span><span class="p">(</span><span class="n">regex</span><span class="p">,</span><span class="n">ky</span><span class="p">):</span>
            <span class="k">return</span> <span class="n">val</span>
    <span class="k">return</span> <span class="kc">None</span></div>

<div class="viewcode-block" id="Crawler"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler">[docs]</a><span class="k">class</span> <span class="nc">Crawler</span><span class="p">(</span><span class="n">CrawlerBase</span><span class="p">):</span>
    <span class="sd">&quot;&quot;&quot;A class to work with an Opendap server&quot;&quot;&quot;</span>
    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">catalogurl</span><span class="p">,</span> <span class="nb">filter</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;dataset&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="s2">&quot;urlPath&quot;</span><span class="p">),</span> <span class="n">followfilter</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;catalogRef&quot;</span><span class="p">)</span><span class="o">.</span><span class="n">OR</span><span class="p">(</span><span class="s2">&quot;dataset&quot;</span><span class="p">)):</span>
        <span class="nb">super</span><span class="p">()</span><span class="o">.</span><span class="fm">__init__</span><span class="p">(</span><span class="n">url</span><span class="o">=</span><span class="n">catalogurl</span><span class="p">)</span>
        <span class="c1">#load the root catalog</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_catalogurl</span><span class="o">=</span><span class="n">catalogurl</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_rootxml</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">getCatalog</span><span class="p">(</span><span class="n">catalogurl</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">services</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">getServices</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">_rootxml</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">_catalogurl</span><span class="p">)</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filt</span><span class="o">=</span><span class="nb">filter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span><span class="o">=</span><span class="n">followfilter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resuming</span><span class="o">=</span><span class="kc">False</span>
<div class="viewcode-block" id="Crawler.setResumePoint"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.setResumePoint">[docs]</a>    <span class="k">def</span> <span class="nf">setResumePoint</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="nb">filter</span><span class="p">,</span><span class="n">followfilt</span><span class="o">=</span><span class="kc">None</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Sets the filters after which the normal filters will be applied.&quot;&quot;&quot;</span>
        <span class="c1"># stores a copy of the normal filter and set the resume filter to the normal filter</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filtcopy</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_filt</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filt</span><span class="o">=</span><span class="nb">filter</span>

        <span class="k">if</span> <span class="n">followfilt</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_followFiltcopy</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span><span class="o">=</span><span class="n">followfilt</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resuming</span><span class="o">=</span><span class="kc">True</span></div>

<div class="viewcode-block" id="Crawler.unsetResumePoint"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.unsetResumePoint">[docs]</a>    <span class="k">def</span> <span class="nf">unsetResumePoint</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Unset resume point&quot;&quot;&quot;</span>
        <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">resuming</span><span class="p">:</span>
            <span class="k">return</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">_filt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_filtcopy</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">_filtcopy</span><span class="o">=</span><span class="kc">None</span>

        <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_followFiltcopy</span><span class="p">:</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_followFiltcopy</span>
            <span class="bp">self</span><span class="o">.</span><span class="n">_followFiltcopy</span><span class="o">=</span><span class="kc">None</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">resuming</span><span class="o">=</span><span class="kc">False</span></div>

<div class="viewcode-block" id="Crawler.getCatalog"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.getCatalog">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">getCatalog</span><span class="p">(</span><span class="n">url</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Retrieve a catalogue&quot;&quot;&quot;</span>
        <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">info</span><span class="p">(</span><span class="s2">&quot;getting Thredds catalog: </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">url</span><span class="p">))</span>
        <span class="n">buf</span><span class="o">=</span><span class="n">http</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">.</span><span class="n">buffer</span><span class="p">()</span>
        <span class="k">return</span> <span class="n">XMLTree</span><span class="o">.</span><span class="n">fromstring</span><span class="p">(</span><span class="n">buf</span><span class="o">.</span><span class="n">getvalue</span><span class="p">())</span></div>


<div class="viewcode-block" id="Crawler.getServices"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.getServices">[docs]</a>    <span class="nd">@staticmethod</span>
    <span class="k">def</span> <span class="nf">getServices</span><span class="p">(</span><span class="n">catalog</span><span class="p">,</span><span class="n">rooturl</span><span class="p">,</span><span class="n">depth</span><span class="o">=</span><span class="mi">2</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Retrieves the root for serving files over http url from a catalogue&quot;&quot;&quot;</span>
        <span class="n">compoundfilt</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;service&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="s2">&quot;serviceType&quot;</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="s2">&quot;Compound&quot;</span><span class="p">)</span>
        <span class="n">opendapfilt</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;service&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="s2">&quot;serviceType&quot;</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="s2">&quot;(OpenDAP)|(OPENDAP)|(DODS)&quot;</span><span class="p">)</span>
        <span class="n">httpfilt</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;service&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="s2">&quot;serviceType&quot;</span><span class="p">,</span> <span class="n">regex</span><span class="o">=</span><span class="s2">&quot;HTTPServer&quot;</span><span class="p">)</span>
        <span class="c1"># possibly add other serices if deemed usefull</span>
        <span class="n">servtuple</span><span class="o">=</span><span class="n">namedtuple</span><span class="p">(</span><span class="s2">&quot;Service&quot;</span><span class="p">,</span><span class="s2">&quot;baseurl opendap catalog http&quot;</span><span class="p">)</span>

        <span class="n">proto</span><span class="p">,</span><span class="n">url</span><span class="o">=</span><span class="n">rooturl</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;://&#39;</span><span class="p">)</span>
        <span class="c1">#also strip off trailing directory to retrieve the root</span>
        <span class="n">baseurl</span><span class="p">,</span> <span class="n">tmp</span><span class="o">=</span><span class="n">url</span><span class="o">.</span><span class="n">split</span><span class="p">(</span><span class="s1">&#39;/&#39;</span><span class="p">,</span> <span class="mi">1</span><span class="p">)</span>
        <span class="n">baseurl</span><span class="o">=</span><span class="n">proto</span><span class="o">+</span><span class="s2">&quot;://&quot;</span><span class="o">+</span><span class="n">baseurl</span>
        <span class="k">if</span> <span class="n">depth</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">depth</span><span class="o">-=</span><span class="mi">1</span>

        <span class="k">for</span> <span class="n">elem</span> <span class="ow">in</span> <span class="n">catalog</span><span class="p">:</span>
            <span class="k">if</span> <span class="n">elem</span><span class="o">.</span><span class="n">tag</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;service&quot;</span><span class="p">):</span>

                <span class="k">if</span> <span class="n">opendapfilt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">elem</span><span class="p">):</span>
                    <span class="n">opendap</span><span class="o">=</span><span class="n">elem</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s2">&quot;base&quot;</span><span class="p">]</span>

                <span class="k">if</span> <span class="n">httpfilt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">elem</span><span class="p">):</span>
                    <span class="n">http</span><span class="o">=</span><span class="n">elem</span><span class="o">.</span><span class="n">attrib</span><span class="p">[</span><span class="s2">&quot;base&quot;</span><span class="p">]</span>

                <span class="k">if</span> <span class="n">compoundfilt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">elem</span><span class="p">):</span>
                    <span class="c1"># allow recursion</span>
                    <span class="k">return</span> <span class="n">Crawler</span><span class="o">.</span><span class="n">getServices</span><span class="p">(</span><span class="n">elem</span><span class="p">,</span><span class="n">rooturl</span><span class="p">,</span> <span class="n">depth</span><span class="p">)</span>

        <span class="n">catalogbase</span><span class="o">=</span><span class="n">rooturl</span><span class="o">.</span><span class="n">strip</span><span class="p">(</span><span class="s1">&#39;catalog.xml&#39;</span><span class="p">)[</span><span class="nb">len</span><span class="p">(</span><span class="n">baseurl</span><span class="p">):]</span>
        <span class="k">return</span> <span class="n">servtuple</span><span class="p">(</span><span class="n">baseurl</span><span class="o">=</span><span class="n">baseurl</span><span class="p">,</span><span class="n">opendap</span><span class="o">=</span><span class="n">opendap</span><span class="p">,</span><span class="n">catalog</span><span class="o">=</span><span class="n">catalogbase</span><span class="p">,</span><span class="n">http</span><span class="o">=</span><span class="n">http</span><span class="p">)</span></div>


<div class="viewcode-block" id="Crawler.xmlitems"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.xmlitems">[docs]</a>    <span class="k">def</span> <span class="nf">xmlitems</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">xmlcatalog</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">url</span><span class="o">=</span><span class="kc">None</span><span class="p">,</span> <span class="n">depth</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generator which returns xml nodes which obey a certain filter</span>
<span class="sd">        Nodes which obey the followFilter will be recursively searched&quot;&quot;&quot;</span>

        <span class="k">if</span> <span class="n">depth</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span>
            <span class="c1"># signals a stopiteration</span>
            <span class="k">return</span>
        <span class="k">else</span><span class="p">:</span>
            <span class="n">depth</span><span class="o">-=</span><span class="mi">1</span>

        <span class="k">if</span> <span class="n">xmlcatalog</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">xmlcatalog</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_rootxml</span>
        <span class="k">if</span> <span class="n">url</span> <span class="ow">is</span> <span class="kc">None</span><span class="p">:</span>
            <span class="n">url</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">_catalogurl</span>

        <span class="k">for</span> <span class="n">xelem</span> <span class="ow">in</span> <span class="n">xmlcatalog</span><span class="p">:</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_filt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">xelem</span><span class="p">):</span>

                <span class="c1">#special check whether we&#39;re considering a resume here</span>
                <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">resuming</span><span class="p">:</span>
                    <span class="bp">self</span><span class="o">.</span><span class="n">unsetResumePoint</span><span class="p">()</span>
                    <span class="c1"># we&#39;re going to continue with the element after this one</span>
                    <span class="k">continue</span>

                <span class="c1"># Allright we can return this entry straight away</span>
                <span class="k">yield</span> <span class="n">xelem</span>
                <span class="c1"># Also continue with the loop after yielding</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="ow">not</span> <span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span><span class="p">:</span>
                <span class="c1"># continue with the next element if no element should be followed</span>
                <span class="k">continue</span>

            <span class="k">if</span> <span class="bp">self</span><span class="o">.</span><span class="n">_followFilt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">xelem</span><span class="p">):</span>
                <span class="c1"># If this is the case we may need a recursive search in either a</span>
                <span class="k">if</span> <span class="n">xelem</span><span class="o">.</span><span class="n">tag</span><span class="o">.</span><span class="n">endswith</span><span class="p">(</span><span class="s2">&quot;catalogRef&quot;</span><span class="p">):</span>

                    <span class="c1"># We treat CatalogRefs in a special way by retrieving the subcatalog from the thredds server</span>
                    <span class="n">suburl</span><span class="o">=</span><span class="n">os</span><span class="o">.</span><span class="n">path</span><span class="o">.</span><span class="n">dirname</span><span class="p">(</span><span class="n">url</span><span class="p">)</span><span class="o">+</span><span class="s2">&quot;/&quot;</span><span class="o">+</span><span class="n">gethref</span><span class="p">(</span><span class="n">xelem</span><span class="p">)</span>
                    <span class="k">try</span><span class="p">:</span>
                        <span class="n">subxml</span><span class="o">=</span><span class="bp">self</span><span class="o">.</span><span class="n">getCatalog</span><span class="p">(</span><span class="n">suburl</span><span class="p">)</span>
                    <span class="k">except</span><span class="p">:</span>
                        <span class="c1"># Just ignore this catalog entry upon exceptions</span>
                        <span class="n">slurplogger</span><span class="p">()</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s2">&quot;Ignoring failed CatalogRef </span><span class="si">%s</span><span class="s2">&quot;</span><span class="o">%</span><span class="p">(</span><span class="n">suburl</span><span class="p">))</span>
                        <span class="k">continue</span>
                <span class="k">else</span><span class="p">:</span>
                    <span class="c1"># Otherwise we&#39;re just going to look in the children of the current element</span>
                    <span class="n">suburl</span><span class="o">=</span><span class="n">url</span>
                    <span class="n">subxml</span><span class="o">=</span><span class="n">xelem</span>

                <span class="k">yield from</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmlitems</span><span class="p">(</span><span class="n">subxml</span><span class="p">,</span> <span class="n">suburl</span><span class="p">,</span> <span class="n">depth</span><span class="p">)</span></div>

<div class="viewcode-block" id="Crawler.uris"><a class="viewcode-back" href="../../../reference/geoslurp.datapull.html#geoslurp.datapull.thredds.Crawler.uris">[docs]</a>    <span class="k">def</span> <span class="nf">uris</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span><span class="n">depth</span><span class="o">=</span><span class="mi">10</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;Generates a list of threddsURI&#39;s (makes use of xmlitems())&quot;&quot;&quot;</span>
        <span class="n">urlFilt</span><span class="o">=</span><span class="n">ThreddsFilter</span><span class="p">(</span><span class="s2">&quot;dataset&quot;</span><span class="p">,</span> <span class="n">attr</span><span class="o">=</span><span class="s2">&quot;urlPath&quot;</span><span class="p">)</span>
        <span class="k">for</span> <span class="n">xelem</span> <span class="ow">in</span> <span class="bp">self</span><span class="o">.</span><span class="n">xmlitems</span><span class="p">(</span><span class="n">depth</span><span class="o">=</span><span class="n">depth</span><span class="p">):</span>
            <span class="k">if</span> <span class="n">urlFilt</span><span class="o">.</span><span class="n">isValid</span><span class="p">(</span><span class="n">xelem</span><span class="p">):</span>
                <span class="k">yield</span> <span class="n">Uri</span><span class="p">(</span><span class="n">xelem</span><span class="p">,</span><span class="bp">self</span><span class="o">.</span><span class="n">services</span><span class="p">)</span></div></div>


</pre></div>

           </div>
           <div class="articleComments">
            
           </div>
          </div>
          <footer>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2020, Roelof Rietbroek.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../../../',
            VERSION:'v1.1.0',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true,
            SOURCELINK_SUFFIX: '.txt'
        };
    </script>
      <script type="text/javascript" src="../../../_static/jquery.js"></script>
      <script type="text/javascript" src="../../../_static/underscore.js"></script>
      <script type="text/javascript" src="../../../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../../../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>